<template>
  <a-modal
    v-model="visible"
    title="选择管理员作为联系人"
    destroyOnClose
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    centered
    @ok="submit"
    :afterClose="afterClose"
  >
    <a-form-model ref="form" :model="form" :rules="rules" :label-col="{ span: 4 }" :wrapper-col="{ span: 18 }">
      <a-form-model-item label="选择人员" prop="userId">
        <a-select v-model="form.userId" placeholder="请选择">
          <a-select-option v-for="item in attache_list" :value="item.userId" :key="item.userId">
            <span class="">{{ item.userName }}</span>
          </a-select-option>
        </a-select>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import { ContactAddOrUpdate } from '@/api/activity/activity'
import { getUserList } from '@/api/system/system'
export default {
  data() {
    return {
      visible: false,
      form: {
        userId: undefined
      },
      rules: {
        userId: [{ required: true, message: '请选择人员', trigger: 'change' }]
      },
      confirmLoading: false,
      type: 'add',
      titleMap: {
        add: '新增',
        edit: '编辑'
      },
      rowData: {},
      attache_list: []
    }
  },
  methods: {
    openModel() {
      this.getList()
      this.visible = true
    },
    closeModel() {
      this.visible = false
    },
    afterClose() {
      this.$refs.form.resetFields()
    },
    getList() {
      getUserList().then(res => {
        this.attache_list = res.rows || []
      })
    },
    submit() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.confirmLoading = true
          try {
            let item = this.attache_list.find(f => f.userId === this.form.userId)
            let res = await ContactAddOrUpdate({ name: item.userName, phone: item.phonenumber })
            this.confirmLoading = false
            // this.$message.success(`新增成功`)
            this.closeModel()
            this.$emit('success', res.data.id)
          } catch (error) {
            this.confirmLoading = false
          }
        }
      })
    }
  }
}
</script>

<style></style>
